package com.yanqu.road.dao.impl.cross.banquet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.autoopen.BanquetAutoOpenRecord;
import com.yanqu.road.entity.banquet.autoopen.BanquetAutoOpenSettleMember;
import com.yanqu.road.entity.banquet.autoopen.UserAutoBanquetActivityData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserAutoBanquetActivityDataDaoImpl extends TempDataObjectDao<UserAutoBanquetActivityData> {

    public static String getUserAutoBanquetActivityDataSql = "select * from `t_u_auto_open_banquet_activity_data` where `activityId`=?";
    public Map<Long, UserAutoBanquetActivityData> getUserAutoBanquetActivityData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserAutoBanquetActivityData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserAutoBanquetActivityDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserAutoBanquetActivityData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateUserAutoBanquetActivityDataSql = "update `t_u_auto_open_banquet_activity_data` set banquetType=?,preReduceGoods=?,curTimes=?,allTimes=?,openRecordList=?,settleMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserAutoBanquetActivityData userAutoBanquetActivityData) {
        userAutoBanquetActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getBanquetType()));
        para.put(i++, new DbParameter(Types.VARCHAR, userAutoBanquetActivityData.getPreReduceGoods()));
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getCurTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getAllTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userAutoBanquetActivityData.getOpenRecordList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userAutoBanquetActivityData.getSettleMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userAutoBanquetActivityData.getUserId()));
        if (getDb().executeNoneQuery(updateUserAutoBanquetActivityDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserAutoBanquetActivityDataSql = "insert into `t_u_auto_open_banquet_activity_data`(activityId,userId,banquetType,preReduceGoods,curTimes,allTimes,openRecordList,settleMap) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserAutoBanquetActivityData userAutoBanquetActivityData) {
        userAutoBanquetActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userAutoBanquetActivityData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getBanquetType()));
        para.put(i++, new DbParameter(Types.VARCHAR, userAutoBanquetActivityData.getPreReduceGoods()));
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getCurTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userAutoBanquetActivityData.getAllTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userAutoBanquetActivityData.getOpenRecordList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userAutoBanquetActivityData.getSettleMap())));
        if (getDb().executeNoneQuery(insertUserAutoBanquetActivityDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserAutoBanquetActivityData parseData(ResultSet rs) throws SQLException {
        UserAutoBanquetActivityData userAutoBanquetActivityData = new UserAutoBanquetActivityData();
        userAutoBanquetActivityData.setActivityId(rs.getInt("activityId"));
        userAutoBanquetActivityData.setUserId(rs.getLong("userId"));
        userAutoBanquetActivityData.setBanquetType(rs.getInt("banquetType"));
        userAutoBanquetActivityData.setPreReduceGoods(rs.getString("preReduceGoods"));
        userAutoBanquetActivityData.setCurTimes(rs.getInt("curTimes"));
        userAutoBanquetActivityData.setAllTimes(rs.getInt("allTimes"));
        userAutoBanquetActivityData.setOpenRecordList(JSON.parseObject(rs.getString("openRecordList"), new TypeReference<List<BanquetAutoOpenRecord>>() {}));
        userAutoBanquetActivityData.setSettleMap(JSON.parseObject(rs.getString("settleMap"), new TypeReference<Map<String, List<BanquetAutoOpenSettleMember>>>() {}));

        return userAutoBanquetActivityData;
    }
}
